var req;
var userMsgId = "usermsg";
var passwordMsgId = "passwordmsg";
var oldPsdMsgId = "oldpasswordmsg";
var confirmPsdMsgId = "confirmpsdmsg";
var emailMsgId = "emailmsg";
var groupMsgId = "groupmsg";
var realNameMsgId = "realnamemsg";
var validateCodeMsgId = "validatecodemsg";

var userValid = false;
var oldpsdValid = false;
var passwordValid = false;
var psdIsSame = false;
var emailValid = false;
var groupNameValid = false;
var realNameValid = false;
var validateCodeValid = false;

var valid_classname = "valid";
var invalid_classname = "invalid";

var commitBtn = null;

/**
 * 验证注册界面的输入框是否都有效
 * @returns {Boolean}
 */
function validateValid(){
	var submitBtn = document.getElementById("submitId");
	commitBtn = submitBtn;
	commitBtn.disabled = true;
	var validFlag = userValid && passwordValid && emailValid && psdIsSame 
		&& groupNameValid && realNameValid && validateCodeValid;
	commitBtn.disabled = false;
	return validFlag;
}

/**
 * 验证注册界面的输入框是否都有效
 * @returns {Boolean}
 */
function validateUpdatePsdValid(){
	return passwordValid && psdIsSame && oldpsdValid;
}

/**
 * 登录名可用验证
 */
function validateLoginName(loginField) {
	if(!notEmpty(loginField)){
		setMessage(userMsgId, "用户名不能为空", invalid_classname);
		userValid = false;
		return;
	}
	
	if(validateLength(loginField, 5)){
		setMessage(userMsgId, "用户名的长度不能小于6", invalid_classname);
		userValid = false;
		return;
	}
	
	if(!validateLength(loginField, 15)){
		setMessage(userMsgId, "用户名的长度不能大于15", invalid_classname);
		userValid = false;
		return;
	}
	
    var url = "validate.jsp?loginName="+ escape(loginField.value);
     
    req = getRequest();

    req.open("GET",url,true);
    req.onreadystatechange = nameCallback;
    req.send(null);
}

function getRequest(){
	if(window.XMLHttpRequest){
    	//针对FireFox，MOzillar，Opear，Safari，IE7，IE8这些浏览器
    	req = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){
    	//针对IE5，IE6 
    	//两个可以用于创建XMLHttpRequest对象的控件名称，保存在一个js的数组中 
    	var activeName = [ "MSXML2.XMLHTTP","Microsoft.XMLHTTP" ];
    	//= new ActiveXObject(""); 
    	for ( var i = 0; i < activeName.length; i++ ){ 
	    	//取出一个空间名进行创建，如果创建成功，种植 
	    	//如果创建失败，则抛出异常，然后可以继续循环，继续尝试创建 
	    	try { 
	    		req = new ActiveXObject( activeName[i] ); 
	    	} catch ( e ) { 
	    	} 
    	}
    }
	
	return req;
}

function nameCallback() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var msg = req.responseXML.getElementsByTagName("msg")[0];
			setNameMsg(msg.childNodes[0].nodeValue);
		}
	}
}

function setNameMsg(msg) {
	if (msg == "validate") {
		setMessage(userMsgId, "恭喜！您可以使用当前用户名注册。", valid_classname);
		userValid = true;
	} else {
		setMessage(userMsgId, "对不起！当前用户名已经被注册，请重新输入用户名。", invalid_classname);
		userValid = false;
	}
}

/**
 * 邮箱可用验证
 */
function validateEmail(emailField) {
	if(!notEmpty(emailField)){
		setMessage(emailMsgId, "邮箱不能为空", invalid_classname);
		emailValid = false;
		return;
	}
	if(!isEmail(emailField)){
		setMessage(emailMsgId, "邮箱格式不正确!", invalid_classname);
		emailValid = false;
		return;
	}
	
    var url = "validate.jsp?email="+ escape(emailField.value);
     
    req = getRequest();

    req.open("GET",url,true);
    req.onreadystatechange = emialCallback;
    req.send(null);
}

function emialCallback() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var msg = req.responseXML.getElementsByTagName("msg")[0];
			setEmailMsg(msg.childNodes[0].nodeValue);
		}
	}
}

function setEmailMsg(msg) {
	if (msg == "validate") {
		setMessage(emailMsgId, "当前邮箱可使用。", valid_classname);
		emailValid = true;
	} else {
		setMessage(emailMsgId, "对不起！该邮箱已被注册，请重新输入。", invalid_classname);
		emailValid = false;
	}
}

/**
 * 验证密码
 * @param passwordField
 */
function validatePassword(passwordField) {
	var msg = "";
	if(!notEmpty(passwordField)){
		msg = "密码不能为空";
	}else if(validateLength(passwordField, 5)){
		msg = "密码不能小于6位";
	}else if(!validateLength(passwordField, 18)){
		msg = "密码不能大于18位";
	}
	
	if(msg == ""){
		passwordValid = true;
		setMessage(passwordMsgId, "", valid_classname);
	}else{
		setMessage(passwordMsgId, msg, invalid_classname);
		passwordValid = false;
	}
	validatePsdIsSame();
}

function validateOldPassword(passwordField){
	var msg = "";
	if(!notEmpty(passwordField)){
		msg = "密码不能为空";
	}else if(validateLength(passwordField, 5)){
		msg = "密码不能小于6位";
	}else if(!validateLength(passwordField, 18)){
		msg = "密码不能大于18位";
	}
	
	if(msg != ""){
		setMessage(oldPsdMsgId, msg, invalid_classname);
	}else{
		var url = "validate.jsp?password="+ escape(passwordField.value);
	    req = getRequest();
	    req.open("GET",url,true);
	    oldPsdValid = false;
	    req.onreadystatechange = passwordCallback;
	    req.send(null);
	}
	
	
}

function validatePsdIsSame(){
	var psdField = document.getElementById("password");
	var confirmPsdField = document.getElementById("confirmPassword");
	if(psdField.value != confirmPsdField.value){
		setMessage(confirmPsdMsgId, "输入的密码不一致", invalid_classname);
		psdIsSame = false;
		return false;
	}else{
		setMessage(confirmPsdMsgId, "", valid_classname);
		psdIsSame = true;
		return true;
	}
}

function passwordCallback() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var msg = req.responseXML.getElementsByTagName("msg")[0];
			setPasswordMsg(msg.childNodes[0].nodeValue);
		}
	}
}

function setPasswordMsg(msg) {
	if (msg == "validate") {
		setMessage(oldPsdMsgId, "密码正确", valid_classname);
		oldpsdValid = true;
	} else {
		setMessage(oldPsdMsgId, "对不起！密码错误", invalid_classname);
		oldpsdValid = false;
	}
}

/**
 * 验证小组
 * @param nameField
 */
function validateGroupName(nameField){
	var msg = "";
	if(!notEmpty(nameField)){
		msg = "小组名称不能为空";
	}
	
	if(msg != ""){
		setMessage(groupMsgId, msg, invalid_classname);
		groupNameValid = false;
		return;
	}else{
		setMessage(groupMsgId, msg, valid_classname);
		groupNameValid = true;
	}
}

/**
 * 验证真实姓名
 * @param nameField
 */
function validateRealName(nameField){
	var msg = "";
	if(!notEmpty(nameField)){
		msg = "真实姓名不能为空";
	}else if(validateLength(nameField, 1)){
		msg = "真实姓名的长度必须大于1";
	}else if(!validateLength(nameField, 5)){
		msg = "真实姓名的长度必须小于5";
	}else if(!isChinese(nameField)){
		msg = "真实姓名必须是中文";
	}
	
	if(msg != ""){
		realNameValid = false;
		setMessage(realNameMsgId, msg, invalid_classname);
		return;
	}else{
		realNameValid = true;
		setMessage(realNameMsgId, msg, valid_classname);
	}
}

/**
 * 验证验证码
 * @param nameField
 */
function validateValCode(field){
	var msg = "";
	if(!notEmpty(field)){
		msg = "验证码不能为空";
	}
	if(msg != ""){
		validateCodeValid = false;
		setMessage(validateCodeMsgId, msg, invalid_classname);
	}else{
		validateCodeValid = true;
	}
}

/**
 * 长度验证
 * @param item 输入框对象
 * @param length
 * @returns {Boolean}
 */
function validateLength(item, length)  
{
	if(item.value.length > length)
	{
		return false;
	}
	return true;
}

/**
 * 只能是英文字幕
 */
function isEnglish(item)
{
	var value = item.value;
	
	if(!(event.keyCode>=65&&event.keyCode<=90))
		event.returnvalue=false;
	var name = item.value;
	for(i = 0; i < name.length; i++) {   
		if(name.charCodeAt(i) >= 65 && name.charCodeAt(i) <= 90)
			return true;
	}
	return false;
}

function isChinese(item) //中文值检测
{   
	var name = item.value;
	for(i = 0; i < name.length; i++) {   
		if(name.charCodeAt(i) <= 128)
			return false;
	}
	return true;
}

/**
 * 是不是邮箱的格式
 * @param item 输入框对象
 * @returns
 */
function isEmail(item) {
	if (item.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else{
		item.focus();
		return false;
	}
}

/**
 * 验证两个输入值是否相等
 * @param item1 输入框对象1
 * @param item2 输入框对象2
 * @returns {Boolean}
 */
function validateTwoInputSame(item1,item2)
{  
	if(item1.value != item2.value)
	{
		return false;
	}
	return true;
}

/**
 * 验证输入值是否为空
 * @param item 输入框对象
 * @returns {Boolean}
 */
function notEmpty(item){
	var value = escape(item.value);
	if(value == "")
		return false;
	return true;
}

/**
 * 设置提示信息
 * @param itemId 对象的Id
 * @param msg 提示字数串
 * @param className 样式名称
 */
function setMessage(itemId, msg, className){
	var mdiv = document.getElementById(itemId);
	mdiv.innerHTML = "<span class=\"" + className +"\"> "+ msg + "</span>";
}

